/*
  Travis Tarr
  tarrt@onid.oregonstate.edu
  CS362-400
  Homework #4

  Testing cardEffect:  mine

*/


#include <stdlib.h>
#include <stdio.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include <assert.h>
#include "rngs.h"
#include <time.h>
#include <string.h>

#define MAX_CARD 26
#define STARTING_HAND 5
#define NULL_CHOICE3 -1
#define DEF_BONUS 10

void setupHand(struct gameState *state, int player){
  int i, card;
  
  // one of the cards needs to be 'mine'
  state->hand[player][0] = mine;
  
  // rest are random
  for(i = 1; i < STARTING_HAND; i++){
    card = rand() % MAX_CARD;
    state->hand[player][i] = card;
  }
  state->handCount[player] = STARTING_HAND;
}

int inHand(struct gameState *state, int player, int card){
  int i;
  for(i = 0; i < STARTING_HAND; i++){
    if(state->hand[player][i] == card){
      return 1;
    }
  }
  return 0;
}

int inDiscard(struct gameState *state, int player, int card){
  int i;
  for(i = 0; i < state->discardCount[player]; i++){
    if(state->discard[player][i] == card){
      return 1;
    }
  }
  return 0;
}

int main(int argc, char*argv[]) {
  time_t tm;
  srand((unsigned) time(&tm));

  int i = 0, player = 0, numPlayers = 2, currentPlayer, choice1, choice2, result;
  int bonus = DEF_BONUS, handPos = 0;
  struct gameState state; // = malloc(size(struct gameState));
  int r, seed = 1000;
  int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
  
  for(i = 0; i < 5; i++){
    memset(&state, 23, sizeof(struct gameState));
    r = initializeGame(numPlayers, k, seed, &state);
    setupHand(&state, player);

    choice1 = -1; // 0 = curse card
    // choice1 = treasure card to trash
    while(inHand(&state, player, choice1) == 0){
      choice1 = rand() % STARTING_HAND;
    }

    // choice2 = card to gain costing up to 3 more
    choice2 = rand() % MAX_CARD;

    // mine returns -1 if choice2 is > last card in list(26)
    // mine returns -1 if choice1 is < copper(4) OR > gold(6)
    // mine returns -1 if cost of (choice1 card + 3) > cost of choice2 card

    result = cardEffect(mine, choice1, choice2, NULL_CHOICE3, &state, handPos, &bonus);
    printf("TEST Iteration: %d, Player: %d", i, player);
    if(result == 0){
      if(inHand(&state, player, choice2) == 1){
        printf("TEST PASSED: Choice2 card in hand");
      } else {
        printf("TEST FAILED: Choice2 card not in hand");
      }
      // make sure mine was discarded
      if(inHand(&state, player, mine) == 0){
        printf("TEST PASSED: mine card no longer in hand");
        if(inDiscard(&state, player, mine) == 0){
          printf("TEST PASSED: mine card in discard pile");
        } else {
          printf("TEST FAILED: mine card not in discard pile");
        }
      } else {
        printf("TEST FAILED: mine card still in hand");
      }
    }
  }

  return 0;
}
